<?php
include('action.abs.php');

class page_manager_mod extends action 
{
	var $pm = null;
	
	const OFFICAL_WB_ID = 5;	// 微博频道组件ID

	function page_manager_mod() 
	{
		parent :: action();
		$this->pm = APP::N('pageManager');
	}

	
	function default_action() 
	{
		$list = $this->pm->get();
		TPL::assign('pages', $list);
		$this->_display('pagelist');
	}

	
	
	/**
	 * 页面组件开关设置
	 */
	function set() 
	{
		$page_id = V('g:page_id');
		$pmId 	 = V('g:c');
		$use 	 = V('g:use');

		if ($page_id && $pmId)
		{
			$result = $this->pm->onOff($page_id, $pmId, $use);
			if ($result) {
				DD('PageModule.getPageModules');
			}
		}

		APP::redirect(URL('mgr/page_manager.setting', array('id'=>$page_id)), 3);
	}

	
	
	/** 
	 * 保存排序
	 */
	function savesort() 
	{
		$ids 		= V('p:ids');
		$page_id 	= V('g:page_id');
		$pos 		= V('g:pos');

		$manager 	= APP::N('pageManager');
		$result 	= $manager->setSort($ids, $page_id, $pos);

		APP::ajaxRst($result, $result ? 0: 1);
		exit;
	}

	
	
	function setting() 
	{
		$page_id = V('g:id');
		if (!$page_id) {
			exit('param page_id missing');
		}

		$page 		= DS('PageModule.getPage', '', $page_id);
		$list 		= DS('PageModule.getPageModules', '', $page_id);
		$modules 	= DR('PageModule.groupByPos', '', $list, null);

		TPL::assign('page_id', $page_id);
		TPL::assign('page', $page);
		TPL::assign('main_modules', isset($modules[1]) ? $modules[1]:null);
		TPL::assign('side_modules', isset($modules[2]) ? $modules[2]:null);

		$this->_display('page_setting');
	}
	
	
	
	/**
	 * \brief 页面组件增加页面
	 */
	function createComponentView()
	{
		$page_id		= V('g:page_id');
		$componentType  = V('g:componentType');
		$url  			= URL('mgr/page_manager.setting', array('id'=>$page_id));
		$componentList	= $this->pm->getCustomeComponent($componentType);
		
		TPL::assign('componentList', $componentList);
		TPL::assign('page_id', $page_id);
		$this->_display('pageManager_createComponentView');
	}
	
	
	
	/**
	 * \brief do the create component action
	 */
	function doCreateComponent()
	{
		$page_id = V('p:page_id');
		$data 	 = V('p:data');
		if (empty($page_id) || empty($data['component_id']) )
		{
			return $this->_error('操作失败, 页面ID或 组件ID不能为空！', array('default_action'));
		}
		
		// organize data
		$data['page_id']	= $page_id;
		$data['in_use']		= 0;
		$data['isNative']	= 0;
		
		$component			= DS('PageModule.getComponent', '', $data['component_id']);
		$data['title']		= $data['title'] ? $data['title'] : $component['title'];
		$data['position']	= $component['component_type'];
		
		$db 	 			= APP::ADP('db');
		$data['sort_num']	= $db->getOne('select max(sort_num) from '. $db->getTable(T_PAGE_MANAGER)) + 1;
		
		// add user group for 微博频道 
		$param 	= array();
		$url 	= URL('mgr/page_manager.setting', array('id'=>$page_id));
		if (self::OFFICAL_WB_ID == $data['component_id'])
		{
			// 微薄list_id
			$list = DR('components/officialWB.getNewListId');
			if(isset($list['rst']['id'])) 
			{
				$param['list_id'] = $list['rst']['id'];
				$rs = DR('mgr/userRecommendCom.addSort', '', $data['title'].'用户列表', 4);
				if($rs['rst']) {
					$param['group_id'] = $rs['rst'];
				}
			} 
			else 	// 不能获取list id, 
			{
				if ($list['errno'] == 1) {
					return $this->_error('操作失败, 已达上限', $url);
				}
				return $this->_error('操作失败, 请检查一下参数', $url);
			}
		}
		$data['param'] = json_encode($param);
		
		// set db and return
		if ($db->save($data, '', T_PAGE_MANAGER))
		{
			return $this->_succ('操作已成功', $url);
		}
		
		return $this->_error('操作失败, 请检查一下参数', $url);
	}
	
	
	
	/**
	 * \brief 页面组件设置页
	 */
	function editComponentView()
	{
		$page_id = V('g:page_id');
		$pmId 	 = V('g:id');
		$url 	 = URL('mgr/page_manager.setting', array('id'=>$page_id));
		$data 	 = $this->pm->getPageManager($pmId);
		if (empty($data))
		{
			return $this->_error('找不到对应组件,请检查一下参数', $url);
		}
		
		$page 			= DS('PageModule.getPage', '', $page_id);
		$component 		= DS('PageModule.getComponent', '', $data['component_id']);
		$data['param'] 	= $this->getEditViewParam($data['param'], $data['component_id']);
		$data['title'] 	= $data['title'] ? $data['title'] : $component['title'];
		
		// assign vars' value
		TPL::assign('data', $data);
		TPL::assign('pmId', $pmId);
		TPL::assign('page', $page);
		TPL::assign('page_id', $page_id);
		TPL::assign('component', $component);
		$this->_display('pageManager_editComponent');
	}
	
	/**
	 * \ 获取 组件编辑页面的param参数，主要是和组件的默认值合并
	 * @param array $param
	 * @param id $componentId
	 */
	private function getEditViewParam($param, $componentId)
	{
		$param = json_decode($param, TRUE);
		$param = empty($param) ? array() : $param;
		
		$comCfg = DR('PageModule.configList', '', FALSE, $componentId);
		$comCfg = empty($comCfg) ? array() : $comCfg;
		
		return array_merge($comCfg, $param);
	}
	
	
	/**
	 * \brief 保存页面组件属性的修改
	 */
	function doEditComponent()
	{
		// get vars
		$pmId = V('p:pmId');
		$url  = URL('mgr/page_manager.setting', array('id'=>V('g:page_id')));
		if (!$pmId || !$aPm = $this->pm->getPageManager($pmId))
		{
			return $this->_error('找不到对应组件,请检查一下参数', $url);
		}
		$data = V('p:data');
		
		// build the param vars
		$param = V('p:param');
		if (isset($param['topic_get']) && 1<=$param['topic_get']) 
		{
			$param['topic_id'] = '0';
			if (2==$param['topic_get'] && 6==$aPm['component_id']) {	// 站内热门话题
				$param['source'] = '1';
			}
		}
		
		// 随便看看要设置默认值
		if( 9 == $aPm['component_id']) {
			DS('components/pubTimeline.config', '', 'source', $param['source']);
			DR('components/pubTimeline.clearCfgCache');
		}
		
		if (is_array($param))
		{
			$pmParam		= json_decode($aPm['param'], TRUE);
			$pmParam		= is_array($pmParam) ? $pmParam : array();
			$data['param']  = json_encode(array_merge($pmParam, $param));
		}
		
		// update and return
		$db 	= APP::ADP('db');
		$result = $db->save($data, $pmId, T_PAGE_MANAGER);
		if ($result || $result===0) 
		{
			$this->delComponentCache($aPm['component_id']);
			$this->_succ('操作已成功', $url);
		} else {
			$this->_error('操作失败！', $url);
		}
	}
	
	
	
	/**
	 * \delete the cache when update success
	 */
	private function delComponentCache($componentId)
	{
		switch ($componentId)
		{
			case 1:		// 热门转发评论
				DD('components/hotWB.getRepost');
				DD('components/hotWB.getComment');
			break;
			
			case 2:		// 名人推荐
				DD('components/star.get');
			break;
			
			case 3:		// 用户推荐
				DD('components/recommendUser.get');
			break;
			
			case 5:		// 微博频道
				DD('components/officialWB.get');
			break;
			
			case 6:		// 推荐话题
				DD('components/hotTopic.get');
			break;
			
			case 7:		// 猜你喜欢
				DD('components/guessYouLike.get');
			break;
			
			case 8:		// 同城微博
				DD('components/cityWB.get');
			break;
			
			case 9:		// 随便看看
				DD('components/pubTimeline.get');
			break;
			
			case 10:	// 今日话题
				DD('components/todayTopic.get');
			break;
			
			case 12:	// 话题微博
				DD('components/todayTopic.getTopicWB');
			break;
			
		}
	}
	
	
	/**
	 * \brief 删除页面组件
	 */
	function delComponent()
	{
		$pmId 	= V('g:pmId');
		$db 	= APP::ADP('db');
		$url	= URL('mgr/page_manager.setting', array('id'=>V('g:page_id')));
		
		if ($pmId && $data = $db->get($pmId, T_PAGE_MANAGER)) 
		{
			if (empty($data['isNative']))
			{
				// delete the 微博频道用户列表
				$this->delOfficalWbUserGroup($data);
				
				// delete the page_manager record
				if ($db->delete($pmId, T_PAGE_MANAGER)) {
					return $this->_succ('操作已成功', $url);
				}
			} else {
				$this->_error('温馨提示：不能删除系统预设组件', $url);
			}
		} 
		$this->_error('操作失败，请检查输入参数是否正确', $url);
	}
	
	
	
	/**
	 * \brief delete the offical wb group if the PM's componennt is officalWB
	 * @param array $aPm
	 */
	private function delOfficalWbUserGroup($aPm)
	{
		if (self::OFFICAL_WB_ID != $aPm['component_id']) {
			return TRUE;
		}
		
		// delete 微博频道 用户列表
		$param = json_decode($aPm['param'], TRUE);
		if (isset($param['group_id'])) {
			DR('mgr/userRecommendCom.delById', '', $param['group_id']);
		}
		
		// delete API上的用户列表
		if (isset($param['list_id'])) {
			DR('components/officialWB.delUserList', '', $param['list_id']);
		}
	}
}
